package com.webshop.DataDao;

import com.webshop.JavaBean.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.SQLException;

public class UserDataBase {
    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());

    /**
     * 个人中心修改资料用的
     * @param name
     * @return
     * @throws Exception
     */
    public User usermodify(String name)throws Exception{
        User user=new User();
       user= qr.query("select * from consumer where name = ?", new BeanHandler<>(User.class), name);
        return user;
    }

    /**
     * 个人中心修改信息
     * @param phone
     * @param email
     * @param name
     * @throws Exception
     */
    public void updateper(String phone,String email,String name)throws Exception{
        String sql="update consumer set" +
                " phone = ? " +
                ", mail = ? "+
                "where name = ? ";
        Object[] p={phone,email,name};
        qr.update(sql,p);
    }
    /**
     * 插入用户信息
     */
    public void insertUser (User user)
    {
        String sql = "insert into consumer(name,password,phone,mail,sex) values(?,?,?,?,?)";
        Object[] parm = {user.getName(), user.getPassword(), user.getPhone(), user.getMail(), user.getSex()};
        try {
            qr.update(sql, parm);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断数据库中是否已经存在用户
     * @param name
     * @return
     */
    public boolean isHaveName(String name)
    {
        int a=0;
        try {
            //由于会返回第一列，所以注意可能会有坑。
            Object obj= qr.query("select count(*) from consumer where name = ?", new ScalarHandler(), name);
            a = Integer.parseInt(String.valueOf(obj));
        } catch (Exception e) {
            return false;
        }

        if(a==0)
            return false;
        return true;

    }

    /**
     * 判断数据库中是否已经存在电话
     * @param phone
     * @return
     */
    public boolean isHavePhone(String phone)
    {
        int a=0;
        try {
            //由于会返回第一列，所以注意可能会有坑。
            Object obj= qr.query("select count(*) from consumer where phone = ?", new ScalarHandler(), phone);
            a = Integer.parseInt(String.valueOf(obj));
        } catch (Exception e) {
            return false;
        }

        if(a==0)
            return false;
        return true;

    }


    /**
     * 判断数据库中是否已经存在邮箱
     * @param email
     * @return
     */
    public boolean isHaveEmail(String email)
    {
        int a=0;
        try {
            //由于会返回第一列，所以注意可能会有坑。
            Object obj= qr.query("select count(*) from consumer where mail = ?", new ScalarHandler(), email);
            a = Integer.parseInt(String.valueOf(obj));
        } catch (Exception e) {
            return false;
        }

        if(a==0)
            return false;
        return true;

    }

    /**
     * 返回用户信息
     * @return
     */
    public User getUser(String name)
    {
        User b = new User();
        try {
            b = qr.query("select * from consumer where name = ?", new BeanHandler<>(User.class), name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * Dao层的登录状态
     * @param name
     * @param password
     * @return
     */
    public boolean login(String name, String password) {
        String sql = "select * from consumer where name = ?";
        try {
            User temp = qr.query(sql, new BeanHandler<>(User.class), name);
            //如果用户为空，（用户不存在）或者密码不争取，返回错误
            if (temp == null || !temp.getPassword().equals(password)) {
                return false;
            }
            return true;
        } catch (Exception e) {
           System.out.println(e.toString());
            return false;
        }
    }

    /**
     * 验证用户名字与邮箱
     * @param name
     * @param email
     * @return
     */
    public boolean justifyEmail(String name, String email) {
        String sql = "select * from consumer where name = ?";
        try {
            User temp = qr.query(sql, new BeanHandler<>(User.class), name);
            if (temp != null && temp.getMail().equals(email)) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    /**
     * 修改密码
     * @param name
     * @param password
     */
    public void changePassword(String name, String password) {
        String sql="update consumer set password = ? WHERE name = ?";
        try {
           int a= qr.update(sql, password, name);
            System.out.println("返回值为 " + a);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
